<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/7/23
 * Time: 14:52
 */
namespace app\jujin\controller;

use app\admin\service\SystemCateService;
use controller\JunjinAdmin;
use service\LogService;
use service\ToolsService;
use service\WechatService;
use think\Db;
use think\Exception;
use think\view\driver\Think;

class Demand extends JunjinAdmin
{
    public $table='member_demand';

    public function index()
    {
        $this->title = '需求管理';
        $get = $this->request->get();
        $db = Db::name($this->table);
        if (isset($get['title']) && $get['title'] !== '') {
            $db->whereLike('title', $get['title']);
        }
        return parent::_list($db->order('id desc'));
    }
    protected function _form_filter(&$data)
    {
        if($this->request->isGet()){
            if($this->request->get('id')){
                $this->title='编辑需求信息';
            }else{
                $this->title='添加需求信息';
            }
        }else{
            $data['endtime']=strtotime($data['endtime']);
            //判断是否推送微信消息
            if(isset($data['is_notice'])&&$data['status']==1){
                if($data['type']==32){
                    $db=Db::name('supplier_info')
                        ->alias('a')
                        ->whereRaw('FIND_IN_SET('.$data['extend_type'].',a.supplier_type_ids)');
                }else{
                    $db=Db::name('worker_info')
                        ->alias('a')
                        ->whereRaw('FIND_IN_SET('.$data['extend_type'].',a.work_type_ids)');
                }
                $members=$db->join('store_member m','m.id=a.mid')
                    ->where('m.status',1)
                    ->where('m.id','<>',$data['mid'])
                    ->order('m.credit desc')
                    ->field('m.id,m.openid')
                    //->limit(10)
                    ->select();
                $redis=ToolsService::get_redis();
                foreach ($members as $k=>$v){
                    $param['touser']=$v['openid'];
                    $param['template_id']='twOVRZIZBjdk_OO0QeA8D7zCqzTdYUPL3CpxukIaLo0';
                    $param['page']='pages/nf_demand_list/nf_demand_list';
                    $param['form_id']=$redis->rPop('jujin_wemini_form_id_list_'.$v['id']);
                    $param['data']=[
                        'keyword1'=>[
                            'value'=>$data['title']
                        ],
                        'keyword2'=>[
                            'value'=>$data['address']
                        ]
                    ];
                    if(!$param['form_id']){
                        LogService::write('模板消息',$v['id'].':FormId不足，微信推送失败');
                        break;
                    }
                    try{
                        $tmp=WechatService::WeMiniTemplate()->send($param);
                    }catch (Exception $e){
                        LogService::write('模板消息',$e->getMessage());
                    }
                }

            }

        }
    }

}